package ca.kooki.codesnippet;

import java.io.File;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Test;

import ca.kooki.codesnippet.core.ActionResponse;
import ca.kooki.codesnippet.core.User;

public class FileSystemCodeSnippetRepositoryTest extends CodeSnippetRepositoryTest {

  @Override
  public CodeSnippetRepository newRepository() 
  {
    String filename = "deleteme.txt";
    return new FileSystemCodeSnippetRepository(filename);
  }

  @After
  public void tearDown()
  {
    super.tearDown();
    (new File("deleteme.txt")).delete();
    (new File("deleteme.txt.users")).delete();
    (new File("deleteme.txt.searches")).delete();
    (new File("deleteme.txt.actions")).delete();
    (new File("findme/deleteme.txt")).delete();
    (new File("findme")).delete();
    
    for (int i=0; i<5; i++)
    {
      (new File("deleteme.txt." + i)).delete();  
    }

    Assert.assertEquals(false,new File("deleteme.txt").exists());
    Assert.assertEquals(false,new File("deleteme.txt.users").exists());
    Assert.assertEquals(false,new File("deleteme.txt.5").exists());
    Assert.assertEquals(false,new File("deleteme.txt.searches").exists());
    Assert.assertEquals(false,new File("deleteme.txt.actions").exists());
  }
  
  @Test
  public void CreatesSnippetFile()
  {
    FileSystemCodeSnippetRepository repository = new FileSystemCodeSnippetRepository("deleteme.txt");
    Assert.assertEquals(false, new File("deleteme.txt").exists());
    
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    repository.addCodeSnippet(snippet);
    Assert.assertEquals(true, new File("deleteme.txt").exists());
  }

  @Test
  public void CreatesUserFile()
  {
    FileSystemCodeSnippetRepository repository = new FileSystemCodeSnippetRepository("deleteme.txt");
    Assert.assertEquals(false, new File("deleteme.txt.users").exists());
    
    User user = new User("aforward");
    repository.addUser(user);
    Assert.assertEquals(true, new File("deleteme.txt.users").exists());
  }

  @Test
  public void CreatesTagFile()
  {
    FileSystemCodeSnippetRepository repository = new FileSystemCodeSnippetRepository("deleteme.txt");
    CodeSnippet snippet = new CodeSnippet("t","k","c");

    Tag tag = new Tag("t2","k2",snippet,"aforward");
    repository.addTag(tag);
    Assert.assertEquals(true, new File("deleteme.txt.1").exists());

    Tag another = new Tag("t3","k3",snippet,"james");
    ActionResponse response = repository.addTag(another);
    
    Assert.assertEquals(true,response.isValid());
    Assert.assertEquals(true, new File("deleteme.txt.2").exists());

  }
  
  @Test
  public void CreateTagDirectory()
  {
    new FileSystemCodeSnippetRepository("findme/deleteme.txt");
    Assert.assertEquals(true, new File("findme").exists());
  }

  @Test
  public void RepositoryDirectoryIsValid()
  {
    
    String fullFilename = new File("deleteme.txt").getAbsolutePath();
    
    CodeSnippetRepository repo = new FileSystemCodeSnippetRepository(fullFilename);
    
    ActionResponse response = repo.isValid();
    Assert.assertEquals(true, response.isValid());
  }

  
  @Test
  public void RepositoryDirectoryNotValid()
  {
    CodeSnippetRepository repo = new FileSystemCodeSnippetRepository("x:\\noway\\delete.txt");
    
    ActionResponse response = repo.isValid();
    Assert.assertEquals(false, response.isValid());
    Assert.assertEquals(true, response.isError("unable_create_repository_directory"));
    Assert.assertEquals(true, response.isError("unable_create_repository_directory",new String[] {"x:\\noway"}));
  }

  @Test
  public void RepositoryFilenameNotValid()
  {
    CodeSnippetRepository repo = new FileSystemCodeSnippetRepository("?*<>:");
    
    ActionResponse response = repo.isValid();
    Assert.assertEquals(false, response.isValid());
    Assert.assertEquals(true, response.isError("problem_with_repository_file"));
    Assert.assertEquals(true, response.isError("problem_with_repository_file",new String[]{"?*<>:","The filename, directory name, or volume label syntax is incorrect"}));
  }

}
